﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Wicresoft.EKA.MVCExtensions;
using Wicresoft.EKA.ITSM.Business;
using Ninject;
using Wicresoft.EKA.ITSM.BusinessImpl;

namespace Wicresoft.EKA.ITSM.Web.WebPortal.Controllers
{
    public class InspectionTaskAPIController : BaseAPIController
    {
        [Inject]
        public ISMGXJPlanTicket SMGXJPlanTicket { get; set; }

        [Inject]
        public ISMGXJPlanTicketRuleRelated SMGXJPlanTicketRuleRelated { get; set; }

        [Inject]
        public ISMGXJPlanObject SMGXJPlanObject { get; set; }
        
        [Inject]
        public ISMGXJPlanRule SMGXJPlanRule { get; set; }

        [Inject]
        public ISMGXJPlanTask SMGXJPlanTask { get; set; }

        [Inject]
        public ISMGXJPlanTaskFrame SMGXJPlanTaskFrame { get; set; }

        [Inject]
        public ISMGXJGroupAsso SMGXJGroupAsso { get; set; }

        [Inject]
        public override IUserInfo UserInfo { get; set; }  //每个Controller必须包含的。

        protected override void Init()
        {
            ((IBase)SMGXJPlanTicket).UserIdentify = UserName;
            ((IBase)SMGXJPlanTicketRuleRelated).UserIdentify = UserName;
            ((IBase)SMGXJPlanTask).UserIdentify = UserName;
            ((IBase)SMGXJPlanObject).UserIdentify = UserName;
            ((IBase)SMGXJPlanRule).UserIdentify = UserName;
            ((IBase)SMGXJPlanTaskFrame).UserIdentify = UserName;
            ((IBase)SMGXJGroupAsso).UserIdentify = UserName;
            base.Init();
        }

        protected override void Dispose(bool disposing)
        {
            ((IBase)SMGXJPlanTicket).Dispose();
            ((IBase)SMGXJPlanTask).Dispose();
            ((IBase)SMGXJPlanTicketRuleRelated).Dispose();
            ((IBase)SMGXJPlanObject).Dispose();
            ((IBase)SMGXJPlanRule).Dispose();
            ((IBase)SMGXJPlanTaskFrame).Dispose();
            ((IBase)SMGXJGroupAsso).Dispose();
            base.Dispose(disposing);
        }

        //生成Task
        public object Post([FromBody] SMGXJPlanTaskModel model2)
        {
            string message = string.Empty;
            try
            {
                Init();
                string GTicketID = SMGXJPlanTask.GetTicketID();
                SMGXJPlanTicketModel model = SMGXJPlanTicket.GetList("\'Request ID\' = \"" + model2.RequestID + "\"").FirstOrDefault();
                //存大单子
                SMGXJPlanTaskFrameModel modelF = new SMGXJPlanTaskFrameModel();
                modelF.Submitter = UserName;
                modelF.ShortDescription = "GenerateTask " + GTicketID;
                modelF.TcChr_Department = model.cChr_Department;
                modelF.TcChr_Description = model.cChr_Description;
                modelF.TcChr_FullName = UserPreferences.FirstOrDefault().Value;
                modelF.TcChr_MarkAsDelete = model.cChr_MarkAsDelete;
                modelF.TcChr_Name = model.cChr_Name;
                modelF.TcChr_Office = model.cChr_Office;
                modelF.cChr_officeName = SMGXJGroupAsso.GetGroupIDNameBy(model.cChr_Office);
                modelF.cChr_TicketName = model.cChr_Name;
                modelF.TcChr_TicketID = model.cChr_TicketID;
                modelF.TaskID = GTicketID;
                modelF.TcChr_Office = model.cChr_Office;
                modelF.cDat_TicketTime = model.CreateDate.ToString("yyyy-MM-dd HH:mm:ss");
                SMGXJPlanTaskFrame.CreateFrame(modelF);
                //先缓存所有数据
                List<SMGXJPlanRuleModel> PlanRulelist = SMGXJPlanRule.GetList("1=1").ToList();
                //List<SMGXJPlanObjectModel> PlanObjectlist = SMGXJPlanObject.GetList("1=1").ToList();
                //List<SMGXJPlanTicketRuleRelatedModel> PlanRelatedlist = SMGXJPlanTicketRuleRelated.GetList("\'cChr_TicketID\' = \"" + model.cChr_TicketID + "\" AND \'cChr_MarkAsDelete\' = \"No\"").ToList();
                List<SMGXJPlanTaskModel> PlanTasklist = new List<SMGXJPlanTaskModel>();

                var list = SMGXJPlanObject.GetList("'cChr_RelatedTicketID' = \"" + model.cChr_TicketID + "\" AND \'cChr_MarkAsDelete\' = \"No\"").OrderBy(o => o.cChr_OrderID);
                //var list = SMGXJPlanTicketRuleRelated.GetList("\'cChr_TicketID\' = \"" + model.cChr_TicketID + "\" AND \'cChr_MarkAsDelete\' = \"No\"");
                //循环CI
                foreach (var related in list)
                {
                    var list2 = related.cChr_IndicatorIDs.Split(';');
                    //循环Rule
                    foreach (var RelatedRule in list2)
                    {
                        //避免多次访问数据库
                        var PlanRule = PlanRulelist.Where(o => o.RequestID == RelatedRule).FirstOrDefault();
                        if (PlanRule != null)
                        {
                            SMGXJPlanTaskModel modelLast = new SMGXJPlanTaskModel();
                            //系统字段赋值
                            modelLast.Submitter = UserName;
                            modelLast.ShortDescription = "GenerateTask " + GTicketID;
                            //Ticket表赋值
                            modelLast.TcChr_Department = model.cChr_Department;
                            modelLast.TcChr_Description = model.cChr_Description;
                            modelLast.TcChr_FullName = model.cChr_FullName;
                            modelLast.TcChr_MarkAsDelete = model.cChr_MarkAsDelete;
                            modelLast.TcChr_Name = model.cChr_Name;
                            modelLast.TcChr_Office = model.cChr_Office;
                            modelLast.TcChr_TicketID = model.cChr_TicketID;
                            //Rule表赋值
                            modelLast.RcChr_Permission = PlanRule.cChr_Permission;
                            modelLast.RcChr_RuleName = PlanRule.cChr_RuleName;
                            modelLast.RcChr_RuleType = PlanRule.cChr_RuleType;
                            modelLast.RcChr_RuleValue = PlanRule.cChr_RuleValue;
                            modelLast.RcChr_Threshold = PlanRule.cChr_Threshold;
                            modelLast.RcChr_RuleRequestID = PlanRule.RequestID;
                            //Object表赋值
                            modelLast.OcChr_Classification = related.cChr_Classification;
                            modelLast.OcChr_DeviceName = related.cChr_DeviceName;
                            modelLast.OcChr_DiskLable = related.cChr_DiskLable;
                            modelLast.OcChr_Hostname = related.cChr_Hostname;
                            modelLast.OcChr_Indicators = related.cChr_Indicators;
                            modelLast.OcChr_IP = related.cChr_IP;
                            modelLast.OcChr_Location = related.cChr_Location;
                            modelLast.OcChr_MarkAsDelete = related.cChr_MarkAsDelete;
                            modelLast.OcChr_ObjectRequestID = related.RequestID;
                            modelLast.OcChr_SQLVersion = related.cChr_SQLVersion;
                            modelLast.OcChr_Type = related.cChr_Type;
                            modelLast.OcChr_Use = related.cChr_Use;
                            modelLast.GTicketID = GTicketID;

                            //描述字段按照不同类型拼值
                            switch (related.cChr_Classification)
                            {
                                case "其他": modelLast.OcChr_Char032 = (related.cChr_IP == null ? "" : related.cChr_IP) + "|" + (related.cChr_Location == null ? "" : related.cChr_Location) + "|" + (related.cChr_jigui == null ? "" : related.cChr_jigui); break;
                                case "服务器": modelLast.OcChr_Char032 = (related.cChr_IP == null ? "" : related.cChr_IP) + "|" + (related.cChr_Location == null ? "" : related.cChr_Location); break;
                                case "交换机": modelLast.OcChr_Char032 = (related.cChr_IP == null ? "" : related.cChr_IP); break;
                                case "存储": modelLast.OcChr_Char032 = (related.cChr_IP == null ? "" : related.cChr_IP) + "|" + (related.cChr_DiskLable == null ? "" : related.cChr_DiskLable); break;
                                case "机房保障": modelLast.OcChr_Char032 = (related.cChr_Location == null ? "" : related.cChr_Location); break;
                                case "数据库": modelLast.OcChr_Char032 = (related.cChr_Hostname == null ? "" : related.cChr_Hostname) + "|" + (related.cChr_IP == null ? "" : related.cChr_IP); break;
                                case "安全设备": modelLast.OcChr_Char032 = (related.cChr_IP == null ? "" : related.cChr_IP); break;
                            }
                            modelLast.cChr_Description = related.cChr_Description;
                            modelLast.cChr_jigui = related.cChr_jigui;

                            PlanTasklist.Add(modelLast);
                        }
                    }
                }
                message = SMGXJPlanTask.Create(PlanTasklist).ToString();
                return new
                {
                    message,
                    GTicketID
                };
            }
            catch (Exception e)
            {
                message = "Fail" + e.Message;
                return new
                {
                    message
                };
            }
        }

        //通过ID获取Task
        public object GetbyID(string TicketID)
        {
            Init();
            string Qual = "'GTicketID'=\"" + TicketID + "\"";
            return SMGXJPlanTask.GetList(Qual);
        }

        //更新Task记录的值
        public object Put(string id, [FromBody] List<SMGXJPlanTaskModel> modellist)
        {
            try
            {
                Init();
                string result = string.Empty;
                bool isOver = true;
                foreach (var model in modellist)
                {
                    model.ShortDescription = "Modify";
                    SMGXJPlanTask.Modify(model);

                    //在全部巡检完成后更改状态
                    if (model.RcChr_Char01 == "" || model.RcChr_Char01 == null)
                        isOver = false;
                }
                //if (isOver)
                //{
                //    string TicketID = id;
                //    SMGXJPlanTaskFrameModel model2 = new SMGXJPlanTaskFrameModel();
                //    model2.Status = SMGXJPlanTaskFrameModel.Status_Enum.Closed;
                //    SMGXJPlanTaskFrame.Modify("'TaskID' = \"" + id + "\"", model2);
                //}
                return "Success";
            }
            catch (Exception e)
            {
                return "Fail:" + e.Message;
            }
        }

        //获取TaskFrame
        public object GetTaskList()
        {
            Init();
            string Qual = "1=1";
            return SMGXJPlanTaskFrame.GetFrameList(Qual);
        }

        //获取TaskFrame列表，首页table
        public object GetMyTaskAll(string SubmitterInfo)
        {
            Init();
            //string Qual = "'Submitter' = \"" + GTTaskID + "\" AND \'OcChr_MarkAsDelete\' = \"No\""; 
            string Qual = "'Submitter' = \"" + SubmitterInfo + "\" AND \'TcChr_MarkAsDelete\' = \"No\""; 
            return SMGXJPlanTaskFrame.GetFrameList(Qual);
        }

        //根据条件筛选
        public object GetSearchList(string TenantsName, string cChr_Name, string TcChr_Name, string TcChr_FullName, string cDat_TicketTime, string cDat_TicketEndTime, string CreateDate, string CreateEndDate, string cChr_Status)
        {
            Init();
            //string Qual = "'Submitter'=\"" + GTTaskID + "\"";
            string Qual = "1=1";
            //给当前登陆人加上默认的组搜索限制
            List<string> listGroup = (List<string>)CacheHelper.GetCache("GetUserInRolesByUserIdentify" + UserName);
            if (listGroup != null)
            {
                if (!listGroup.Contains("管理人员"))
                {
                    Qual = Qual + " AND (";
                    foreach (var groupname in listGroup)
                    {
                        Qual += "'cChr_officeName'=\"" + groupname + "\" OR ";
                    }
                    Qual = Qual.TrimEnd(' ').TrimEnd('R').TrimEnd('O') + ")";
                }
            }
            if (cChr_Status != null)
            {
                if (cChr_Status.Equals("异常")) 
                {
                    Qual += "AND 'cChr_Status'=\"" + "abnormal" + "\"";
                } 
                if(cChr_Status.Equals("正常"))
                {
                    Qual += "AND 'cChr_Status'=\"" + "normal" + "\"";
                }
            }
            if (TenantsName!=null)
            {
                Qual += "AND 'cChr_officeName'=\"" + TenantsName + "\"";
            }
            if (cChr_Name != null)
            {
                Qual += "AND 'cChr_TicketName' Like \"%" + cChr_Name + "%\"";
            }
            if (TcChr_Name != null)
            {
                Qual += "AND 'TcChr_Name' Like \"%" + TcChr_Name + "%\"";
            }
            if (TcChr_FullName != null)
            {
                Qual += "AND 'TcChr_FullName' Like \"%" + TcChr_FullName + "%\"";
            }
            if (cDat_TicketTime != null && cDat_TicketEndTime == null)
            {
                Qual += "AND 'cDat_TicketTime'>\"" + cDat_TicketTime + "\"" ;
                //Qual += " AND PhotoTime='" + Stime.Trim() + "'" + " AND PhotoTime='" + Otime.Trim() + "'";
            }
            if (cDat_TicketTime == null && cDat_TicketEndTime != null)
            {
                Qual +="AND 'cDat_TicketTime'<\"" + cDat_TicketEndTime + "\"";
                //Qual += " AND PhotoTime='" + Stime.Trim() + "'" + " AND PhotoTime='" + Otime.Trim() + "'";
            }
            if (cDat_TicketTime != null && cDat_TicketEndTime != null)
            {
                Qual += "AND 'cDat_TicketTime'>\"" + cDat_TicketTime + "\"" + "AND 'cDat_TicketTime'<\"" + cDat_TicketEndTime + "\"";
                //Qual += " AND PhotoTime='" + Stime.Trim() + "'" + " AND PhotoTime='" + Otime.Trim() + "'";
            }
            if (CreateDate != null && CreateEndDate == null)
            {
                Qual += "AND 'Create Date'>\"" + CreateDate + "\"";
            }
            if (CreateDate == null && CreateEndDate != null)
            {
                Qual += "AND 'Create Date'<\"" + CreateEndDate + "\"";
            }
            if (CreateDate != null && CreateEndDate != null)
            {
                Qual += "AND 'Create Date'>\"" + CreateDate + "\"" + "AND 'Create Date'<\"" + CreateEndDate + "\"";
            }
            return SMGXJPlanTaskFrame.GetFrameList(Qual);
        }
       
        //查询异常巡检单信息
        public object GetSearchByList(string TcChr_TicketID,string CreateDateBug)
        {
            Init();
            string Qual = "'TcChr_TicketID'=\"" + TcChr_TicketID + "\"AND 'cChr_Status'=\"" + "abnormal" + "\"AND 'Create Date'<\"" + CreateDateBug + "\"";
            return SMGXJPlanTaskFrame.GetFrameByList(Qual);
        }

        //删除功能
        public object PUT(string TaskID)
        {
            Init();
            SMGXJPlanTaskFrameModel model = new SMGXJPlanTaskFrameModel();
            model.TcChr_MarkAsDelete = "Yes";
            string Qual = "'TaskID'=\"" + TaskID + "\"";
            var id = SMGXJPlanTaskFrame.GetFrame(Qual).RequestID;
            string Qualid = "'Request ID'=\"" + id + "\"";
            //return SMGXJPlanTaskFrame.Modify(id, model);
            return SMGXJPlanTaskFrame.Modify(Qualid, model);
        }
    }
}
